package com.xmall.product.handler.msg;

import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import com.xmall.config.AmqpCommonConfig;
import com.xmall.msg.queue.ProductModule;
import com.xmall.msg.entity.order.OrderCancelMsg;
import com.xmall.product.service.other.IMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 取消订单
 */
@Slf4j
@Component
@RabbitListener(queues = ProductModule.OrderCancel.ORDER_CANCEL_QUEUE, containerFactory = AmqpCommonConfig.RABBIT_LISTENER_CONTAINER_FACTORY_NAME)
public class OrderCancelHandler {

    @Resource
    private IMsgService msgService;

    @RabbitHandler
    public void msg(Channel channel, Message message, OrderCancelMsg msg) throws IOException {
        log.info("rabbitmq handler: product service: OrderCancelHandler: {}", JSONUtil.toJsonStr(msg));
        msgService.sendStockRevertMsg(msg.getOrderId());
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
